﻿(function () {
    var myApp = angular.module('MyControllers');

    // 考场监控列表的控制器及方法列表
    myApp.controller('monitorController', ['$scope', '$interval', '$http', '$modal', 'examMonitorService', 'utils', function ($scope, $interval, $http, $modal, service, utils) {
        var srv = service.examMonitorSrv;

        $scope.getList = function () {
            srv.getList($scope.planType).success(function (data) {
                $scope.data = data;
            });
        };

        $scope.official = function () {
            $scope.planType = 0;
            $scope.getList();
        };

        $scope.imatate = function () {
            $scope.planType = 1;
            $scope.getList();
        };

        $scope.official();

        $scope.detail = function (model) {
            $scope.loadingState = true;

            var parameter = {
                planId: model.examPlanInfoID,
                planName: model.examPlanInfoName,
                roomId: model.examRoomID,
                sceneId: model.examSceneID,
                examMode: model.examMode,
                startDelayTime: model.examSceneStartDelayTime,
                isSeatByNumberRequired: model.isSeatByNumberRequired
            };

            utils.encrypt(JSON.stringify(parameter)).success(function (data) {
                if (data.resultType == 'Success') {
                    window.location.href = '#/examMonitor/detail' + '?parameter=' + data.appendData;
                } else {
                    utils.notify(data.message, 'warning');
                }

                $scope.loadingState = false;
            });
        };
    }]);

    // 明细界面需要的控制器及方法列表
    myApp.controller('monitorDetailController', ['$scope', '$interval', '$location', '$http', '$modal', 'examMonitorService', 'utils', function ($scope, $interval, $location, $http, $modal, service, utils) {
        var planId, roomId, sceneId, examMode, startDelayTime, srv = service.detailSrv;
        $scope.currentPage = 1;
        $scope.pageSize = 10;

        var methods = {
            init: function () {
                $scope.loadingState = true;
                utils.decrypt($location.$$search.parameter).success(function (data) {
                    if (data.resultType == 'Success') {
                        var result = JSON.parse(data.appendData);
                        planId = result.planId;
                        roomId = result.roomId;
                        sceneId = result.sceneId;
                        examMode = result.examMode;
                        startDelayTime = result.startDelayTime;
                        $scope.isAnyTime = examMode == 3;
                        $scope.isSeatByNumberRequired = result.isSeatByNumberRequired == 1;

                        $scope.operList = [
                            { operId: 1, operName: '标记作弊' }
                            ,{ operId: 5, operName: '拍照' }
                            ,{ operId: 6, operName: '查看照片' }
                            /*,{ operId: 0, operName: '强制交卷' }
                            ,{ operId: 2, operName: '考生延时' }
                            ,{ operId: 3, operName: '考生管理' }
                            ,{ operId: 4, operName: '换机考试' }*/
                        ];

                        $scope.planName = result.planName;

                        $scope.getIsRoomDelayed(planId, sceneId, roomId);
                        $scope.getRoomList(roomId, sceneId);

                        // 每分钟刷新一次列表数据
                        var timer3 = $interval(function () {
                            $scope.query();
                        }, 60000);
                        utils.setData({ timer3: timer3 });
                    } else {
                        utils.notify(data.message, 'warning');
                    }

                    $scope.loadingState = false;
                });
            },
            getIsRoomDelayed: function (planId, roomId, subjectId) {
                srv.getIsRoomDelayed(planId, roomId, subjectId).success(function (data) {
                    $scope.isRoomDelayed = data;
                });
            },
            getRoomList: function (roomId, sceneId, subjectId) {
                srv.getRoomList(planId, sceneId, subjectId).success(function (data) {
                    $scope.roomList = data;

                    if (roomId) {
                        angular.forEach($scope.roomList, function (item) {
                            if (roomId == item.examRoomID) {
                                $scope.curRoom = item;
                            }
                        });
                    } else {
                        $scope.curRoom = $scope.roomList[0];
                    }

                    $scope.getSceneList(sceneId, roomId, subjectId);
                });
            },
            getSceneList: function (sceneId, roomId, subjectId) {
                srv.getSceneList(planId, roomId, subjectId).success(function (data) {
                    angular.forEach(data, function (item) {
                        item.examSceneName += ' (' + utils.formatDate(item.planBeginTime) + ')';
                    });

                    $scope.sceneList = data;

                    if (sceneId) {
                        angular.forEach($scope.sceneList, function (item) {
                            if (sceneId == item.examSceneID) {
                                $scope.curScene = item;
                            }
                        });
                    } else {
                        $scope.curScene = $scope.sceneList[0];
                    }

                    $scope.getSubjectList(sceneId, roomId);
                });
            },
            getSubjectList: function (sceneId, roomId) {
                srv.getSubjectList(planId, sceneId, roomId).success(function (data) {
                    data.unshift({ examSubjectID: null, subjectName: '全部科目' });

                    $scope.subjectList = data;
                    $scope.curSubject = $scope.subjectList[0];


                    $scope.getStatusList();
                });
            },
            getStatusList: function () {
                var data = [
                    { StatusId: null, StatusName: '全部状态' },
                    { StatusId: 0, StatusName: '未开考' },
                    { StatusId: 1, StatusName: '已登录' },
                    { StatusId: 2, StatusName: '已开考未交卷' },
                    { StatusId: 3, StatusName: '已交卷' }
                ];

                $scope.statusList = data;
                $scope.curStatus = $scope.statusList[0];
                var anImport = false;
                $scope.loadExamcaseList(anImport, roomId, sceneId,$scope.currentPage,$scope.pageSize);
            },
            query: function (page) {
                if(page == 1){
                    $scope.currentPage = 1;
                }
                var curScene = $scope.curScene;
                var curRoom = $scope.curRoom;
                var curSubject = $scope.curSubject;
                var curStatus = $scope.curStatus;
                var curParams = $scope.curParams;
                var anImport = false;
                $scope.loadExamcaseList(anImport, curRoom.examRoomID, curScene.examSceneID, $scope.currentPage, $scope.pageSize, curStatus.StatusId, curSubject.examSubjectID, curParams);
                $scope.getIsRoomDelayed(planId, curScene.examSceneID, curRoom.examRoomID);
            },
            download: function(){
                var curScene = $scope.curScene;
                var curRoom = $scope.curRoom;
                var curSubject = $scope.curSubject;
                var curStatus = $scope.curStatus;
                var curParams = $scope.curParams;
                var anImport = true;
                $scope.downloadList(anImport, curRoom.examRoomID, curScene.examSceneID, $scope.currentPage, $scope.pageSize, curStatus.StatusId, curSubject.examSubjectID, curParams);

            },
            downloadList: function(anImport, roomId, sceneId, currentPage, pageSize, statusId, subjectId, curParams){
                srv.getExamcaseList(anImport, planId, roomId, currentPage, pageSize, statusId, subjectId, sceneId, curParams).success(function (data) {
                    if( data.ResultType == 'Success'){
                         //动态生成一个form，并通过它实现下载功能
                         var form = $('<form></form>');
                         form.attr('action', '/ExamReport/Download');
                         form.attr('method', 'post');
                         form.css('display', 'none');
                         var input = $('<input type="hidden" name="filePath" value="' + data.Message + '" />');
                         form.append(input);
                         form.appendTo('body');
                         form.submit();
                    }else{
                        utils.notify( data.Message , 'warning');
                    }
                });
            },
            //delayAllExam: function () {
            //    $modal.open({
            //        templateUrl: 'app/views/ExamMonitor/Delay.html',
            //        controller: 'delayController',
            //        backdrop: 'static',
            //        keyboard: false,
            //        resolve: {
            //            params: function () {
            //                return {
            //                    planId: planId,
            //                    roomId: $scope.curRoom.ExamRoomID,
            //                    sceneId: $scope.curScene.ExamSceneID,
            //                    startDelayTime: startDelayTime
            //                };
            //            }
            //        }
            //    });
            //},
            loadExamcaseList: function (anImport, roomId, sceneId, currentPage, pageSize, statusId, subjectId, curParams) {
                srv.getExamcaseList(anImport, planId, roomId, currentPage, pageSize, statusId, subjectId, sceneId, curParams).success(function (data) {
                    $scope.totalPage = data.totalPage;// 总页数
                    $scope.list = data;

                    angular.forEach($scope.list.items, function (data, index, array) {
                        // 统考签到情况
                        if (examMode != 3 && data.iDCardResult > 0) {
                            data.todayIDCard = true;
                        }
                            // 随考签到情况
                        else if (examMode == 3 && data.iDCardResult > 0 && utils.isToday(data.iDCardTime)) {
                            data.todayIDCard = true;
                        } else {
                            data.todayIDCard = false;
                        }
                    });
                });
            },
            // 跳转页
            selectPage: function(){
                //不能小于1大于最大
                if ($scope.currentPage < 1) $scope.currentPage = 1;
                if ($scope.currentPage > $scope.totalPage) $scope.currentPage = $scope.totalPage;
                $scope.query();
            },
            // 上一页
            Previous: function(){
                if($scope.currentPage > 1){
                    $scope.currentPage--;
                    $scope.query();
                }
            },
            // 下一页
            Next: function(){
                if($scope.currentPage < $scope.totalPage){
                    $scope.currentPage++;
                    $scope.query();
                }
            },
            changeRoom: function () {
                $scope.query('1');
            },
            oper: function (model) {
                switch (model.curOper.operId) {
                    case 0:
                        if (model.$parent.item.examStatus == 0) {
                            utils.notify('未开考状态下不能强制交卷!', 'warning');
                        } else if (model.$parent.item.examStatus == 1) {
                            utils.notify('已登录状态下不能强制交卷!', 'warning');
                        } else if (model.$parent.item.examStatus == 3) {
                            utils.notify('已交卷状态下不能强制交卷!', 'warning');
                        } else {
                            $modal.open({
                                templateUrl: '/App/views/Exammonitor/Mandatory.html',
                                controller: 'mandatory',
                                backdrop: 'static',
                                keyboard: false,
                                resolve: {
                                    params: function () {
                                        return model.$parent.item;
                                    }
                                }
                            });
                        }

                        break;
                    case 1:
                        if (model.$parent.item.examStatus == 0) {
                            utils.notify('未开考状态下不能标记作弊!', 'warning');
                        } else {
                            $modal.open({
                                templateUrl: '/App/views/Exammonitor/SignDeregulation.html',
                                controller: 'signDeregulation',
                                backdrop: 'static',
                                keyboard: false,
                                resolve: {
                                    params: function () {
                                        return model.$parent.item;
                                    }
                                }
                            });
                        }

                        break;
                    case 2:
                        if (model.$parent.item.examStatus == 3) {
                            utils.notify('已交卷状态下不能延时!', 'warning');
                        } else {
                            $modal.open({
                                templateUrl: '/App/views/Exammonitor/ExamineeDelayed.html',
                                controller: 'examineeDelayed',
                                backdrop: 'static',
                                keyboard: false,
                                resolve: {
                                    params: function () {
                                        return { item: model.$parent.item, time: startDelayTime }
                                    }
                                }
                            });
                        }

                        break;
                    case 3:
                        alert('考生管理');

                        break;
                    case 4:
                        alert('换机考试');

                        break;
                    case 5:
                        if (model.$parent.item.examStatus == 0) {
                            utils.notify('未开考状态下不能拍照!', 'warning');
                        } else if (model.$parent.item.examStatus == 3) {
                            utils.notify('已交卷状态下不能拍照!', 'warning');
                        } else {
                            $modal.open({
                                templateUrl: '/App/views/Exammonitor/TakePhoto.html',
                                controller: 'takePhoto',
                                backdrop: 'static',
                                keyboard: false,
                                resolve: {
                                    params: function () {
                                        return model.$parent.item;
                                    }
                                }
                            });
                        }

                        break;
                    case 6:
                        $modal.open({
                            templateUrl: '/App/views/Exammonitor/viewPhoto.html',
                            controller: 'viewPhoto',
                            size: 'lg',
                            resolve: {
                                params: function () {
                                    return model.$parent.item;
                                }
                            }
                        });

                        break;
                }
            },
            examineeDetail: function (model) {
                $modal.open({
                    templateUrl: '/App/views/Exammonitor/ExamineeDetail.html',
                    controller: 'examineeDetail',
                    size: 'lg',
                    resolve: {
                        params: function () {
                            return { planId: planId, model: model, isSeatByNumberRequired: $scope.isSeatByNumberRequired }
                        }
                    }
                });
            },
            delayAllExam: function () {
                $scope.sss = true;
            },
            // delayAllExam save
            save: function () {
                if ($('form#delayForm').valid()) {
                    if ($scope.minutes > startDelayTime) {
                        utils.notify('请输入不大于' + startDelayTime + '分钟的延时时长！', 'warning');
                    } else {
                        $scope.loadingState = true;

                        srv.extendedAllExam(planId, sceneId, roomId, $scope.minutes, $scope.reason).success(function (data) {
                            if (data.resultType == 'Success') {
                                $scope.close();

                                utils.notify('整场延时' + $scope.minutes + '分钟，操作成功！', 'success');
                                $scope.isRoomDelayed = true;
                                $scope.sss = false;
                            } else {
                                utils.notify(data.message, 'warning');
                            }
                            $scope.loadingState = false;
                        });
                    }
                }
            },
            close: function () {
                $scope.sss = false;
            }
        };

        angular.extend($scope, methods);
        $scope.init();

        $scope.$root.$on('reloadList', function () {
            $scope.query();
        });
    }]);

    /*
    // 整场延时需要的控制器及方法列表
    myApp.controller('delayController', ['$scope', '$routeParams', '$modalInstance', 'examMonitorService', 'utils', 'params', '$filter', function ($scope, $routeParams, $modalInstance, service, utils, params, $filter) {
        var srv = service.operSrv, startDelayTime = params.startDelayTime;
    
        var methods = {
            save: function () {
                if ($('form#delayForm').valid()) {
                    if ($scope.minutes > startDelayTime) {
                        utils.notify('请输入不大于' + startDelayTime + '分钟的延时时长！', 'warning');
                    } else {
                        $scope.loadingState = true;
    
                        srv.extendedAllExam(params.planId, params.sceneId, params.roomId, $scope.minutes, $scope.reason).success(function (data) {
                            if (data.ResultType == ResultType.Success) {
                                $scope.close();
    
                                utils.notify('整场延时' + $scope.minutes + '分钟，操作成功！', 'success');
                            } else {
                                utils.notify(data.Message, 'warning');
                            }
                            $scope.loadingState = false;
                        });
                    }
                }
            },
            close: function () {
                $modalInstance.close(true);
            }
        };
        angular.extend($scope, methods);
    }]);
    */

    // 强制交卷需要的控制器及方法列表
    myApp.controller('mandatory', ['$scope', '$routeParams', '$modalInstance', 'examMonitorService', 'utils', 'params', '$filter', function ($scope, $routeParams, $modalInstance, service, utils, params, $filter) {
        var srv = service.operSrv;
        $scope.examineeName = params.examineeName;

        var methods = {
            save: function () {
                $scope.loadingState = true;

                srv.mandatoryPaper(params).success(function (data) {
                    if (data.resultType == 'Success') {
                        $scope.close();

                        // 之后刷新列表
                        $scope.$emit('reloadList');

                        utils.notify('强制交卷，操作成功！', 'success');
                    } else {
                        utils.notify(data.message, 'warning');
                    }
                    $scope.loadingState = false;
                });
            },
            close: function () {
                $modalInstance.close(true);
            }
        };
        angular.extend($scope, methods);
    }]);

    // 标记作弊需要的控制器及方法列表
    myApp.controller('signDeregulation', ['$scope', '$routeParams', '$modalInstance', 'examMonitorService', 'utils', 'params', '$filter', function ($scope, $routeParams, $modalInstance, service, utils, params, $filter) {
        var srv = service.operSrv, arrangeId = params.examineeArrangeID;
        $scope.examineeName = params.examineeName;

        var methods = {
            getList: function () {
                $scope.loadingState = true;

                srv.getDeregulationList().success(function (data) {
                    $scope.list = data;
                    $scope.loadingState = false;
                });
            },
            save: function (model) {
                $scope.loadingState = true;

                srv.signDeregulation(arrangeId, model.deregulationItemID).success(function (data) {
                    if (data.resultType == 'Success') {
                        $scope.close();

                        // 之后刷新列表
                        $scope.$emit('reloadList');

                        utils.notify('标记作弊，操作成功！', 'success');
                    } else {
                        utils.notify(data.message, 'warning');
                    }
                    $scope.loadingState = false;
                });
            },
            close: function () {
                $modalInstance.close(true);
            }
        };
        angular.extend($scope, methods);
        $scope.getList();
    }]);

    // 考生明细需要的控制器及方法列表
    myApp.controller('examineeDetail', ['$scope', '$routeParams', '$modalInstance', 'examMonitorService', 'utils', 'params', '$filter', function ($scope, $routeParams, $modalInstance, service, utils, params, $filter) {
        var model = params.model, planId = params.planId, srv = service.operSrv;
        $scope.isSeatByNumberRequired = params.isSeatByNumberRequired;

        var methods = {
            init: function () {
                $scope.info = model;

                $scope.loadingState = true;
                srv.getStudentInfo(planId, model.examineeID,model.examSubjectID).success(function (data) {
                    $scope.baseInfo = data;
                    $scope.loadingState = false;
                    $('.photoImg').attr('src', '/images'+ data.photo).error(function () {
                        this.src = '//openexamcdn.open.com.cn/openexam/static/App/images/Login/defaultPhoto.jpg';
                        this.onerror = null;
                    });
                });
            },
            close: function () {
                $modalInstance.close(true);
            }
        };
        angular.extend($scope, methods);
        $scope.init();
    }]);

    // 考生单个延时需要的控制器及方法列表
    myApp.controller('examineeDelayed', ['$scope', '$routeParams', '$modalInstance', 'examMonitorService', 'utils', 'params', '$filter', function ($scope, $routeParams, $modalInstance, service, utils, params, $filter) {
        var srv = service.operSrv, startDelayTime = params.time;
        $scope.examineeName = params.item.examineeName;

        var methods = {
            save: function () {
                if ($('form#examineeDelayForm').valid()) {
                    if ($scope.minutes > startDelayTime) {
                        utils.notify('请输入不大于' + startDelayTime + '分钟的延时时长！', 'warning');
                    } else {
                        $scope.loadingState = true;

                        srv.extendedExamInfo(params.item.examineeArrangeID, $scope.minutes, $scope.reason, params.item.examCardNo).success(function (data) {
                            if (data.resultType == 'Success') {
                                $scope.close();

                                // 之后刷新列表
                                $scope.$emit('reloadList');

                                utils.notify('考生' + $scope.examineeName + '延时' + $scope.minutes + '分钟，操作成功！', 'success');
                            } else {
                                utils.notify(data.message, 'warning');
                            }
                            $scope.loadingState = false;
                        });
                    }
                }
            },
            close: function () {
                $modalInstance.close(true);
            }
        };
        angular.extend($scope, methods);
    }]);

    // 拍照需要的控制器及方法列表
    myApp.controller('takePhoto', ['$scope', '$routeParams', '$modalInstance', 'examMonitorService', 'utils', 'params', '$filter', function ($scope, $routeParams, $modalInstance, service, utils, params, $filter) {
        var srv = service.operSrv;
        $scope.examineeName = params.examineeName;

        var methods = {
            save: function () {
                $scope.loadingState = true;

                srv.pushMessageToClient(params).success(function (data) {
                    if (data.resultType == 'Success') {
                        $scope.close();

                        // 之后刷新列表
                        $scope.$emit('reloadList');

                        utils.notify('拍照，操作成功！', 'success');
                    } else {
                        utils.notify(data.message, 'warning');
                    }
                    $scope.loadingState = false;
                });
            },
            close: function () {
                $modalInstance.close(true);
            }
        };
        angular.extend($scope, methods);
    }]);

    // 查看照片需要的控制器及方法列表
    myApp.controller('viewPhoto', ['$scope', '$modal', '$routeParams', '$modalInstance', 'examMonitorService', 'utils', 'params', '$filter', function ($scope, $modal, $routeParams, $modalInstance, service, utils, params, $filter) {
        var srv = service.operSrv;
        $scope.examineeInfo = params;

        var methods = {
            init: function () {
                $scope.loadingState = true;

                srv.findPhotoList(params).success(function (data) {
                    $scope.loadingState = false;

                    angular.forEach(data, function (list) {
                        angular.forEach(list, function (item) {
                            // "管理端拍照"=0  "考试抓拍"=1   "登录拍照"=2
                            if($.trim(item.photoResourceType) == ""){
                                if($(".photoList1").html() == ""){
                                    $(".photoList1").append('<dl><dt><img class="photoImg" src="/images'+ item.examinerHeadPhoto +'" onerror="this.src=\'//openexamcdn.open.com.cn/openexam/static/App/images/Login/defaultPhoto.jpg\'" /></dt><dd>报名照片</dd></dl>');
                                }
                            }
                            switch (item.photoResourceType) {

                                case "0":
                                    $(".photoList4").append('<li><p><img class="photoImg" src="/images'+ item.photoResourcePath +'"></p>'
                                        +'<p class="red">相似度：'+ item.score +'%</p>'
                                        +'<p>'+ item.insertTime +'</p></li>');

                                    if($(".photoList1").html() == ""){
                                        $(".photoList1").append('<dl><dt><img class="photoImg" src="/images'+ item.examinerHeadPhoto +'" onerror="this.src=\'//openexamcdn.open.com.cn/openexam/static/App/images/Login/defaultPhoto.jpg\'" /></dt><dd>报名照片</dd></dl>');
                                    }

                                    break;
                                case "1":
                                    $(".photoList3").append('<li><p><img class="photoImg" src="/images'+ item.photoResourcePath +'"></p>'
                                        +'<p class="red">相似度：'+ item.score +'%</p>'
                                        +'<p>'+ item.insertTime +'</p></li>');

                                    if($(".photoList1").html() == ""){
                                        $(".photoList1").append('<dl><dt><img class="photoImg" src="/images'+ item.examinerHeadPhoto +'" onerror="this.src=\'//openexamcdn.open.com.cn/openexam/static/App/images/Login/defaultPhoto.jpg\'" /></dt><dd>报名照片</dd></dl>');
                                    }

                                    break;
                                case "2":
                                    $(".photoList2").append('<dl><dt><img class="photoImg" src="/images'+ item.photoResourcePath +'" />'
                                                            +'<label style="vertical-align: middle;line-height: 2em;margin-left: 20px;">'
                                                            +'<span class="red">相似度：'+ item.score +'%</span></br><span>'+ item.insertTime +'</span>'
                                                            +'</label></dt><dd>登录照片</dd></dl>');

                                    if($(".photoList1").html() == ""){
                                        $(".photoList1").append('<dl><dt><img class="photoImg" src="/images'+ item.examinerHeadPhoto +'" onerror="this.src=\'//openexamcdn.open.com.cn/openexam/static/App/images/Login/defaultPhoto.jpg\'" /></dt><dd>报名照片</dd></dl>');
                                    }

                                    break;
                            }
                        });
                    });
                });
            },
            mandatory: function () {
                if ($scope.examineeInfo.examStatus == 0) {
                    utils.notify('未开考状态下不能强制交卷!', 'warning');
                } else if ($scope.examineeInfo.examStatus == 1) {
                    utils.notify('已登录状态下不能强制交卷!', 'warning');
                } else if ($scope.examineeInfo.examStatus == 3) {
                    utils.notify('已交卷状态下不能强制交卷!', 'warning');
                } else {
                    $modal.open({
                        templateUrl: '/App/views/Exammonitor/Mandatory.html',
                        controller: 'mandatory',
                        backdrop: 'static',
                        keyboard: false,
                        resolve: {
                            params: function () {
                                return $scope.examineeInfo;
                            }
                        }
                    });
                }
            },
            signDeregulation: function () {
                if ($scope.examineeInfo.examStatus == 0) {
                    utils.notify('未开考状态下不能标记作弊!', 'warning');
                } else {
                    $modal.open({
                        templateUrl: '/App/views/Exammonitor/SignDeregulation.html',
                        controller: 'signDeregulation',
                        backdrop: 'static',
                        keyboard: false,
                        resolve: {
                            params: function () {
                                return $scope.examineeInfo;
                            }
                        }
                    });
                }
            },
            close: function () {
                $modalInstance.close(true);
            }
        };
        angular.extend($scope, methods);
        $scope.init();
    }]);
})();